package io.lkt.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BPosterInfoEntity;
import io.lkt.modules.sys.entity.SysGzhEntity;
import io.lkt.modules.sys.service.SysUserRoleService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lkt.modules.sys.entity.BPosterHistoryEntity;
import io.lkt.modules.sys.service.BPosterHistoryService;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;



/**
 * 海报生成记录表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2020-12-01 16:25:19
 */
@RestController
@RequestMapping("/sys/bposterhistory")
public class BPosterHistoryController {
    @Autowired
    private BPosterHistoryService bPosterHistoryService;

    @Autowired
    SysUserRoleService sysUserRoleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:bposterhistory:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bPosterHistoryService.queryPage(params);

        List<?> list = page.getList();

        if (CollectionUtils.isNotEmpty(list)) {
            List<BPosterHistoryEntity> bPosterHistoryList = Lists.newArrayList();

            for (Object temp : list) {
                BPosterHistoryEntity bPosterHistoryEntity = (BPosterHistoryEntity) temp;
                if ((bPosterHistoryEntity.getGzhCode() != null) && (!bPosterHistoryEntity.getGzhCode().equals(""))) {
                    SysGzhEntity sysGzh = this.sysUserRoleService.getGzhByCode(bPosterHistoryEntity.getGzhCode());
                    bPosterHistoryEntity.setGzhName(sysGzh.getGzhName());
                } else {
                    bPosterHistoryEntity.setGzhName("全部平台");
                }

                bPosterHistoryList.add(bPosterHistoryEntity);
            }
            page.setList(bPosterHistoryList);
        }

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:bposterhistory:info")
    public R info(@PathVariable("id") Integer id){
        BPosterHistoryEntity bPosterHistory = bPosterHistoryService.getById(id);

        return R.ok().put("bPosterHistory", bPosterHistory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:bposterhistory:save")
    public R save(@RequestBody BPosterHistoryEntity bPosterHistory){

        bPosterHistory.setCreateTime(new Date());
        bPosterHistory.setUpdateTime(new Date());
        bPosterHistoryService.save(bPosterHistory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:bposterhistory:update")
    public R update(@RequestBody BPosterHistoryEntity bPosterHistory){
        ValidatorUtils.validateEntity(bPosterHistory);

        bPosterHistory.setUpdateTime(new Date());
        bPosterHistoryService.updateById(bPosterHistory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:bposterhistory:delete")
    public R delete(@RequestBody Integer[] ids){
        bPosterHistoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
